#include "Precomp.h"
#include "SpriteBased.h"
#include "SQGame.h"
#include "ResourceManager.h"

SpriteBased::SpriteBased(Entity::EntityType type, const SQChar* path)
:Entity(type)
{
	shape=&shape_impl;
	if(path)
	{
		setImage(path);
	}
	sf::IntRect intRect=shape_impl.GetSubRect();
	size.x=(float)intRect.GetWidth();
	size.y=(float)intRect.GetHeight();
	init();
}

void SpriteBased::serialize(SquirrelObject& out) const
{
	Entity::serialize(out);
	out.SetValue(L"path",path.c_str());
}

void SpriteBased::deserialize(SquirrelObject& in)
{
	Entity::deserialize(in);
	setImage(in.GetString(L"path"));
}

void SpriteBased::setImage(const SQChar* path)
{
	ResourceManager& rm=SQGame::getInstance().getResourceManager();
	shape_impl.SetImage(rm.loadImage(path));
	this->path=path;
}

SpriteBased::~SpriteBased()
{
}